package com.example.ymm.modules1.service.impl.hl.kq;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.KqGroupRule;
import com.example.ymm.modules1.entity.my.user.KqRule;
import com.example.ymm.modules1.mapper.my.user.KqGroupRuleMapper;
import com.example.ymm.modules1.query.kq.KqGroupRuleQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.kq.ViewGroupRule;
import com.example.ymm.modules1.service.dto.heng.kq.ViewKqRule;
import com.example.ymm.modules1.service.hl.kq.KqGroupRuleService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class KqGroupRuleServiceImpl extends BaseServiceImpl<KqGroupRuleMapper, KqGroupRule> implements KqGroupRuleService {



    @Override
    public TableList<ViewGroupRule> list(KqGroupRuleQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<ViewGroupRule> arrs = getGroupRuleList(criteria);
        PageInfo<ViewGroupRule> pageInfo = new PageInfo<>(arrs);
        TableList<ViewGroupRule> tableList = new TableList(pageInfo);
        return tableList;
    }

    public List<KqGroupRule> queryAll(KqGroupRuleQueryCriteria criteria) {
        List<KqGroupRule> result =baseMapper.selectList(QueryHelpPlus.getPredicate(KqGroupRule.class, criteria));
        return result;
    }




    @Override
    public void settingGroupRule(String deptIds, String ruleId) {
        String[] split = deptIds.split(",");
        for (String deptIdId : split) {
            KqGroupRuleQueryCriteria criteria=new KqGroupRuleQueryCriteria();
            criteria.setDeptId(deptIdId);
            criteria.setRuleId(ruleId);
            List<KqGroupRule> kqGroupRules = queryAll(criteria);
            if(UtilValidate.isEmpty(kqGroupRules)){
                KqGroupRule kqGroupRule=new KqGroupRule();
                kqGroupRule.setDeptId(deptIdId);
                kqGroupRule.setRuleId(ruleId);
                saveOrUpdate(kqGroupRule);
            }
        }
    }


    @Override
    public List<ViewGroupRule> getGroupRuleList(KqGroupRuleQueryCriteria criteria) {
        return baseMapper.getGroupRuleList(criteria);
    }

    @Override
    public List<ViewKqRule> getRuleGroupList(KqGroupRuleQueryCriteria criteria) {
        List<ViewKqRule> ruleGroupList = baseMapper.getRuleGroupList(criteria);
        for (ViewKqRule kqRule : ruleGroupList) {
            if(UtilValidate.isNotEmpty(kqRule.getType())){
                if(kqRule.getType()==1){
                    kqRule.setTypeValue("正常班");
                }else if(kqRule.getType()==2){
                    kqRule.setTypeValue("连班");
                }else if(kqRule.getType()==3){
                    kqRule.setTypeValue("加班");
                }
            }
        }
        return ruleGroupList;
    }
}
